package com.loyal.loyalerp.humenresource.contract.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.loyal.loyalerp.humenresource.contract.entity.Contract;
/**
 * @author yangminzi
 * @param ContractMapper.java
 */
@Mapper
public interface ContractMapper {
    
	@Select("select rs_contract.*,gy_person.personName from rs_contract,gy_person where gy_person.id = rs_contract.personId and rs_contract.currentContract=1")
	public List<Contract> selectAllContract();
	
	//根据用户ID查找所有合同
	@Select("select rs_contract.*,gy_person.personName from rs_contract,gy_person where gy_person.id = rs_contract.personId and rs_contract.personId = #{value}")
	public List<Contract> selectAllContractByPersonId(int personId);
	
	//根据用户姓名模糊查找personId
	@Select("select id from gy_person where personName like '%${value}%' ")
	public List<Integer> selectPersonIdByName(String personName);
	
	//添加合同
	@Insert("insert into rs_contract value(null,#{personId},#{contractType},#{startTime},#{endTime},#{officeDate},#{currentContract})")
	public void addContract(Contract contract);
	
	//根据用户code查询出personId
	@Select("select id from gy_person where personCode = #{value}")
	public int selectIdByCode(int code);
	
	//判断当前用户有没有未处理的合同
	@Select("select count(*) from rs_contract where personId = #{personId} and currentContract = 1")
	public int IsCurrentContract(int personId);
	
	//删除合同
	@Delete("delete from rs_contract where id = #{value}")
	public void deleteContract(int id);
		
	//根据合同结束时间降序排列出所有合同
	@Select("SELECT * from rs_contract c WHERE c.personId = #{value} ORDER BY endTime desc")
	public List<Contract> contractDesc(int personId);
	
	@Select("select personId from rs_contract where id= #{value}")
	public int getPersonIdByPrimaryKey(int id);
	
	//根据合同id设置合同为当前合同
	@Update("update rs_contract set currentContract=1 where id = #{value}")
	public void updateCurrentContract(int id);
	
	//修改合同信息
	@Update("update rs_contract set contractType=#{contractType},startTime=#{startTime},endTime=#{endTime},officeDate=#{officeDate},currentContract=#{currentContract} where id=#{id}")
	public void updateContract(Contract contract);
	
	//根据主键查询合同id
	@Select("select * from rs_contract where id = #{value}")
	public Contract selectContractById(int id);
}
